package com.partners.utils;

import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @author liangyongle
 * @date 2018-12-18
 */
public class CodeUtils {


    public static String getNewUserCode(Integer code){
       Integer nextCode = code+1;
       String lastCode = nextCode.toString().substring(6);
       SimpleDateFormat df = new SimpleDateFormat("yyMMdd");//设置日期格式
       String format = df.format(new Date());
       String newCode = format.concat(lastCode);
       return newCode;
   }



   public static String getNewCustomerCode(Long code){
       String lastCode = code.toString().concat("0001");
       return lastCode;
   }

    /**
     *创建新的报备号
     * */
    public static String getNextCustomerCode(Long code) {
        Long nextCode = code+1;
        String lastCode = nextCode.toString();
        return lastCode;
    }

    /**
    *创建新的订单号
    * */
    public static String createOrderNum(){
        SimpleDateFormat sdf=new SimpleDateFormat("YYMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<3;i++){
         result+=random.nextInt(10);
        }
        return newDate+result;
    }


    /**
     *创建新的兑换订单号
     * */
    public static Long createExchangeNum(){
        SimpleDateFormat sdf=new SimpleDateFormat("YYYYMMddHHmm");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<2;i++){
            result+=random.nextInt(11);
        }
        return Long.parseLong(newDate+result);
    }

    /**
     * 创建新的分组订单号
     * */
    public static String createGroupOrderNum(){
        SimpleDateFormat sdf=new SimpleDateFormat("YYYYMMddHHmm");
        String newDate=sdf.format(new Date());
        String code = "";
        String result="";
        Random randoms=new Random();
        for(int i=0;i<3;i++){
            result+=randoms.nextInt(10);
        }
        return "O"+newDate+result;
    }



    /**
     *检查权限
     * */
    public static ModelAndView checkPermissionView(Boolean permission,String viewName, Integer code, Integer uid){
        ModelAndView mav = new ModelAndView();
        if(permission){
            mav.setViewName(viewName);
            mav.addObject("uid",uid);
        }else{
            mav.setViewName("show/error");
            mav.addObject("code",404);
            mav.addObject("msg","无操作权限");
        }
       return mav;
    }




}
